package com.sunday.android.fangdaifu.bukong.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.entity.event.ResetPwd;
import com.sunday.android.fangdaifu.bukong.http.ApiClient;
import com.sunday.android.fangdaifu.bukong.utils.BaseUtil;
import com.sunday.common.event.EventBus;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/29.
 */

public class SettingPwdActivity extends BaseActivity{
    @Bind(R.id.edt_password)
    EditText edtPassword;
    @Bind(R.id.see)
    TextView see;
    @Bind(R.id.btn_confirm)
    TextView btnConfirm;
    private long userId;
    private boolean isSee = false;
    private String pwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivity_set_pwd);
        ButterKnife.bind(this);
        userId = getIntent().getLongExtra("userId",0);
    }
    @OnClick({R.id.btn_confirm,  R.id.see})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                pwd = edtPassword.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)){
                    ToastUtils.showToast(mContext,"请输入新的密码");
                    return;
                }
                showLoadingDialog(0);
                setPwd();
                break;
            case R.id.see:
                see.setSelected(!isSee);
                isSee = !isSee;
                if (isSee) {
                    edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                }
                break;
        }
    }

    private void setPwd(){
        Call<ResultDO> call = ApiClient.getApiAdapter().AdSetPassword(userId,pwd);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext,"重置成功，请重新登录");
                    EventBus.getDefault().post(new ResetPwd());
                    BaseUtil.openActivity(mContext,LoginActivity.class);
                    SettingPwdActivity.this.finish();
                }else {
                    ToastUtils.showToast(mContext , response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
            }
        });
    }
}
